﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyPhongMach.Context;
using QuanLyPhongMach.Platform;
using QuanLyPhongMach.Model;

namespace QuanLyPhongMach.View
{
    /// <summary>
    /// Interaction logic for WTraCuuBenhNhan.xaml
    /// </summary>
    public partial class WTraCuuBenhNhan : Page
    {
        #region Constructor
        public WTraCuuBenhNhan()
        {
            InitializeComponent();
            InitializeAutoTenBenhNhan();
            InitializeAutoDiaChi();
            InitializeAutoTenLoaiBenh();
            InitializeBangKetQua();
            
            TrangThaiMacDinh();
        }

        private void InitializeAutoTenLoaiBenh()
        {
            txtautoLoaiBenh.ItemsSource = CTool.LayDanhSachTenLoaiBenh();
        }

        private void InitializeAutoDiaChi()
        {
            txtautoDiaChi.ItemsSource = CTool.LayDanhSachDiaChi();
        }

        private void InitializeAutoTenBenhNhan()
        {
            txtautoTenBenhNhan.ItemsSource = CTool.LayDanhSachTenBenhNhan();
        }

        private void InitializeBangKetQua()
        {
            GridView grdView = new GridView();
            grdView.AllowsColumnReorder = true;
            grdView.ColumnHeaderToolTip = "BENHNHAN";

            GridViewColumn mabenhnhan = new GridViewColumn();
            mabenhnhan.DisplayMemberBinding = new Binding("MaBenhNhan");
            mabenhnhan.Header = "Mã Bệnh Nhân";
            mabenhnhan.Width = 120;
            grdView.Columns.Add(mabenhnhan);

            GridViewColumn hoten = new GridViewColumn();
            hoten.DisplayMemberBinding = new Binding("HoTen");
            hoten.Header = "Họ Tên";
            hoten.Width = 120;
            grdView.Columns.Add(hoten);

            GridViewColumn gioitinh = new GridViewColumn();
            gioitinh.DisplayMemberBinding = new Binding("StringGioiTinh");
            gioitinh.Header = "Giới Tính";
            gioitinh.Width = 120;
            grdView.Columns.Add(gioitinh);

            GridViewColumn namsinh = new GridViewColumn();
            namsinh.DisplayMemberBinding = new Binding("ShortNamSinh");
            namsinh.Header = "Năm Sinh";
            namsinh.Width = 120;
            grdView.Columns.Add(namsinh);

            GridViewColumn diachi = new GridViewColumn();
            diachi.DisplayMemberBinding = new Binding("DiaChi");
            diachi.Header = "Địa Chỉ";
            diachi.Width = 300;
            grdView.Columns.Add(diachi);

            viewBenhNhan.View = grdView;
        }
        #endregion

        #region Trạng thái
        private void VoHieuNgay()
        {
            dateNamSinhDen.IsEnabled = dateNamSinhTu.IsEnabled = dateNgayKhamTu.IsEnabled = dateNgayKhamDen.IsEnabled = false;
        }

        private void KichHoatNgay()
        {
            dateNamSinhDen.IsEnabled = dateNamSinhTu.IsEnabled = dateNgayKhamTu.IsEnabled = dateNgayKhamDen.IsEnabled = true;
        }

        private void VoHieuThongTin()
        {
            txtautoDiaChi.IsEnabled = false;
            txtautoLoaiBenh.IsEnabled = false;
        }

        private void KichHoatThongTin()
        {
            txtautoDiaChi.IsEnabled = true;
            txtautoLoaiBenh.IsEnabled = true;
        }

        private void TrangThaiMacDinh()
        {
            VoHieuNgay();
            VoHieuThongTin();

            txtautoTenBenhNhan.Focus();

            dateNamSinhDen.SelectedDate = dateNamSinhTu.SelectedDate = DateTime.Now;
            dateNgayKhamTu.SelectedDate = dateNgayKhamDen.SelectedDate = DateTime.Now;
        }

        private void TrangThaiHienThiKetQua()
        {
            expTimKiemNangCao.IsExpanded = false;
            txtautoTenBenhNhan.Focus();
        }
        #endregion

        #region Function
        public void Refresh()
        {
            
        }

        private void CapNhatBangKetQua(List<CBenhNhan> listBenhNhan)
        {
            if (listBenhNhan == null || listBenhNhan.Count == 0)
            {
                MessageBox.Show("Không tìm thấy kết quả", 
                    "Thông báo", 
                    MessageBoxButton.OK, 
                    MessageBoxImage.Information);
            }
            viewBenhNhan.ItemsSource = listBenhNhan;
            txtSoluongTimKiem.Text = string.Format("Tìm được {0} kết quả", listBenhNhan.Count);
        }

        private void TimKiem()
        {
            if (expTimKiemNangCao.IsExpanded)
            {
                TimKiemNangCao();
            }
            else
            {
                TimKiemThongThuong();
            }
            TrangThaiHienThiKetQua();
        }

        private void TimKiemNangCao()
        {
            if (checkTimNgay.IsChecked == true && checkTimThongTin.IsChecked == true)
            {
                CapNhatBangKetQua(CTool.LayThongTinBenhNhanNangCao(txtautoTenBenhNhan.Text,
                    txtautoDiaChi.Text, txtautoLoaiBenh.Text,
                    Convert.ToDateTime(dateNamSinhTu.SelectedDate.Value), Convert.ToDateTime(dateNamSinhDen.SelectedDate.Value),
                    Convert.ToDateTime(dateNgayKhamTu.SelectedDate.Value), Convert.ToDateTime(dateNgayKhamDen.SelectedDate.Value)));
            }
            else
                if (checkTimThongTin.IsChecked == true && checkTimNgay.IsChecked == false)
                {
                    CapNhatBangKetQua(CTool.LayThongTinBenhNhanNangCao(txtautoTenBenhNhan.Text,
                   txtautoDiaChi.Text, txtautoLoaiBenh.Text));
                }
                else
                    if (checkTimThongTin.IsChecked == false && checkTimNgay.IsChecked == true)
                    {
                        CapNhatBangKetQua(CTool.LayThongTinBenhNhanNangCao(
                   Convert.ToDateTime(dateNamSinhTu.SelectedDate.Value), Convert.ToDateTime(dateNamSinhDen.SelectedDate.Value),
                   Convert.ToDateTime(dateNgayKhamTu.SelectedDate.Value), Convert.ToDateTime(dateNgayKhamDen.SelectedDate.Value)));
                    }
                    else
                    {
                        TimKiemThongThuong();
                    }
        }

        private void TimKiemThongThuong()
        {
            if (txtautoTenBenhNhan.Text == "")
            {
                MessageBox.Show("Vui lòng nhập thông tin tìm kiếm",
                    "Thông báo", 
                    MessageBoxButton.OK, 
                    MessageBoxImage.Information);
                return;
            }

            CapNhatBangKetQua(CTool.TimKiemBenhNhanBangTen(txtautoTenBenhNhan.Text));
        }
        #endregion

        #region Event
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TimKiem();
            }
        }

        private void btnTimKiem_Click(object sender, RoutedEventArgs e)
        {
            TimKiem();
        }

        private void btnTimKiem_MouseLeave(object sender, MouseEventArgs e)
        {
            TimKiem();
        }

        private void checkTimThongTin_Checked(object sender, RoutedEventArgs e)
        {
            if (checkTimThongTin.IsChecked == true)
            {
                KichHoatThongTin();
            }
            else
            {
                VoHieuThongTin();
            }
        }

        private void checkTimNgay_Checked(object sender, RoutedEventArgs e)
        {
            if (checkTimNgay.IsChecked == true)
            {
                KichHoatNgay();
            }
            else
            {
                VoHieuNgay();
            }
        }
        #endregion
    }
}
